package com.broada.apm.ajax;

import java.util.List;

import com.broada.apm.entity.IndicatorItem;
import com.broada.apm.entity.TradeErrorCode;
import com.broada.apm.entity.TradeErrorDetail;
import com.broada.apm.model.PagingList;
import com.broada.apm.model.performance.BasePerformance;
import com.google.common.collect.Lists;

public class IndicatorResult extends Result {

	/**
	 * @Fields serialVersionUID :
	 */

	private static final long serialVersionUID = 1L;

	public void setPageData(PagingList<? extends BasePerformance> page) {
		this.addPagingList(page);
	}

	public void setIndictorItems(IndicatorItem[] items) {
		this.put("indicatorConfig", items);
	}
	
	public void setTradeErrorDetail(List<TradeErrorDetail> details) {
		this.put("result", details);
	}
	public void setTradeErrorIds(List<TradeErrorCode> errorIds) {
		this.put("result", errorIds);
	}
	
	public void setTradeErrorIds(TradeErrorDetail detail) {
		this.put("result", detail);
	}
	public IndicatorResult pagingHandle(int pageSize, int pageIndex, List<?> list) {

		if (list == null || list.size() == 0) {
			this.put("result", Lists.newArrayList());
			return this;
		}

		int totalPage = 0;
		int count = list.size();
		int number = count % pageSize;
		this.setPageSize(pageSize);
		this.setTotal(count);
		this.setPageIndex(pageIndex);
		if (number > 0) {
			totalPage = count / pageSize + 1;
		} else {
			totalPage = count / pageSize;
		}
		this.setTotalPage(totalPage);
		if (pageIndex < totalPage - 1) {
			list = list.subList(pageIndex * pageSize, (pageIndex + 1) * pageSize);
		} else {
			list = list.subList((totalPage - 1) * pageSize, list.size());
		}
		this.put("result", list);
		return this;
	}
}
